//
//  OosSearchQuery.m
//  com.oos.kraken
//
//  Created by Pablo Gonzalez on 2/16/11.
//  Copyright 2011 11870.com. All rights reserved.
//

#import "OosSearchQuery.h"
#import "../../Utils/NSStringUtils.h"
#import <oos4ios/ApiUrlProvider.h>

@implementation OosSearchQuery

static NSString *const AREA_SLUG				= @"as";
static NSString *const LOCATION_KEY				= @"l";
static NSString *const TAG_KEY					= @"tag";
static NSString *const OPTIONAL_TAG_KEY			= @"tapOp";
static NSString *const CATEGORY_KEY				= @"category";
static NSString *const ATTRIBUTE_KEY			= @"attribute";
static NSString *const CATEGORY_GROUP_KEY		= @"categoryGroup";
static NSString *const OPTIONAL_CATEGORY_KEY	= @"categoryOp";
static NSString *const LATITUDE_KEY				= @"lat";
static NSString *const LONGITUDE_KEY			= @"lon";
static NSString *const RADIUS_KEY				= @"radius";
static NSString *const BBOX_KEY					= @"bbox";
static NSString *const SEARCH_LOCATION_KEY		= @"sl";
static NSString *const SCOPE_KEY				= @"scope";
static NSString *const ORDER_KEY				= @"order";
static NSString *const REDUCED_KEY              = @"reduced";
static NSString *const FILTERS_KEY              = @"filters";

static NSArray *scopes;
static NSArray *sorts;


+ (void) initialize
{
	if (!scopes) {
		scopes = [NSArray arrayWithObjects: @"global", @"user", @"constans", nil];
	}
	if (!sorts) {
		sorts = [NSArray arrayWithObjects: @"content", @"distance", @"rating", nil];
	}
}

+(OosSearchQuery *) query
{
	NSString *url = [ApiUrlProvider provider].search.url;
	return [[[OosSearchQuery alloc] initWithFeedURL:[NSURL URLWithString:url]] autorelease];
}

- (void) setSearchQuery: (NSString *) searchQuery
{
	[self setFullTextQueryString:searchQuery];
}
- (NSString *) searchQuery
{
	return [self fullTextQueryString]; 
}

- (void) setAreaSlug: (NSString *) areaSlug
{
	[self addCustomParameterWithName:AREA_SLUG value:areaSlug];
}
- (NSString *) areaSlug
{
	return [self valueForParameterWithName:AREA_SLUG];
}

- (void) setLocation: (NSString *) location
{
	[self addCustomParameterWithName:LOCATION_KEY 
							   value:location];
}
- (NSString *) location
{
	return [self valueForParameterWithName:LOCATION_KEY];
}

#pragma mark Tags

- (void) addTag: (NSString *) tag
{
	if (self.tags) {
        self.tags = [NSStringUtils addValue:tag toString:self.tags];
    } else {
        self.tags = tag;
    }
}

- (void) removeTag: (NSString *) tag
{
	if (self.tags) {
		self.tags = [NSStringUtils removeValue:tag fromString:self.tags];
	}
}

- (NSString *) tags
{
	return [self valueForParameterWithName:TAG_KEY];
}

- (void) setTags:(NSString *) tags
{
	[self addCustomParameterWithName:TAG_KEY value:tags];
}

#pragma mark Categories

- (void) addCategory: (NSString *) category
{
    if (self.categories) {
        self.categories = [NSStringUtils addValue:category toString:self.categories];
    } else {
        self.categories = category;
    }
}

- (void) removeCategory:(NSString *)category
{
	if (self.categories) {
		self.categories = [NSStringUtils removeValue:category fromString: self.categories];
	}
}

- (NSString *) categories
{
	return [self valueForParameterWithName:CATEGORY_KEY];
}

- (void) setCategories:(NSString *) category
{
	[self addCustomParameterWithName: CATEGORY_KEY value: category];
}

#pragma mark Attributes

- (void) addAttribute: (NSString *) attribute
{
    if (self.attributes) {
        self.attributes = [NSStringUtils addValue:attribute toString:self.attributes];
    } else {
        self.attributes = attribute;
    }
}

- (void) removeAttribute:(NSString *)attribute
{
	if (self.attributes) {
		self.attributes = [NSStringUtils removeValue:attribute fromString:self.attributes];
	}
}

- (NSString *) attributes
{
	return [self valueForParameterWithName:ATTRIBUTE_KEY];
}

-(void) setAttributes:(NSString *) attribute
{
	[self addCustomParameterWithName: ATTRIBUTE_KEY value: attribute];
}

#pragma mark Category Groups

- (void) addCategoryGroup: (NSString *) categoryGroup
{
    if (self.categoryGroups) {
        self.categoryGroups = [NSStringUtils addValue:categoryGroup toString:self.categoryGroups];
    } else {
        self.categoryGroups = categoryGroup;
    }
}

- (void) removeCategoryGroup:(NSString *)categoryGroup
{
	if (self.categoryGroups) {
		self.categoryGroups = [NSStringUtils removeValue:categoryGroup fromString:self.categoryGroups];
	}
}

- (NSString *) categoryGroups
{
	return [self valueForParameterWithName:CATEGORY_GROUP_KEY];
}

-(void) setCategoryGroups:(NSString *) categoryGroup
{
	[self addCustomParameterWithName: CATEGORY_GROUP_KEY value: categoryGroup];
}



- (void) setLatitude: (NSNumber *) latitude
{
	[self addCustomParameterWithName:LATITUDE_KEY value:[latitude stringValue]];
}
- (NSNumber *) latitude
{
	double result = [[self valueForParameterWithName:LATITUDE_KEY] doubleValue];
	return [NSNumber numberWithDouble:result];
}

- (void) setLongitude: (NSNumber *) longitude
{
	[self addCustomParameterWithName:LONGITUDE_KEY value: [longitude stringValue]];
}
- (NSNumber *) longitude
{
	double result = [[self valueForParameterWithName:LONGITUDE_KEY] doubleValue];
	return [NSNumber numberWithDouble: result];
}

- (void) setRadius: (NSNumber *) radius
{
	[self addCustomParameterWithName:RADIUS_KEY value:[radius stringValue]];
}
- (NSNumber *) radius
{
	double result =  [[self valueForParameterWithName:RADIUS_KEY] doubleValue];
	return [NSNumber numberWithDouble: result];
}

- (void) setBoundaryBox: (NSString *) boundaryBox
{
	// TODO create BoundaryBox Class
	[self addCustomParameterWithName:BBOX_KEY value:boundaryBox];
}
- (NSString *) boundaryBox
{
	return [self valueForParameterWithName:BBOX_KEY];
}

- (void) setLocationSearch: (NSString *) locationSearch 
{
	[self addCustomParameterWithName:SEARCH_LOCATION_KEY value:locationSearch];
}
- (NSString *) locationSearch 
{
	return [self valueForParameterWithName:SEARCH_LOCATION_KEY];
}

- (void) setScope: (NSString *) scope
{
	[self addCustomParameterWithName:SCOPE_KEY value:scope];
}
- (NSString *) scope
{
	return [self valueForParameterWithName:SCOPE_KEY];
}

- (void) setOrder: (NSString *) order
{
	[self addCustomParameterWithName:ORDER_KEY value:order];
}
- (NSString *) order
{
	return [self valueForParameterWithName:ORDER_KEY];
}

- (void)setFilters:(NSString *)filters 
{
    [self addCustomParameterWithName:FILTERS_KEY value:filters];
}
- (NSString *)filters
{
    return [self valueForParameterWithName:FILTERS_KEY];
}
- (void)addFilter:(NSString *)filter
{
    if (self.filters) {
        self.filters = [NSStringUtils addValue:filter toString:self.filters];
    } else {
        self.filters = filter;
    }
}
- (void)removeFilter:(NSString *)filter
{
    if (self.filters) {
        self.filters = [NSStringUtils removeValue:filter fromString:self.filters];
    }
}

- (void) packStore:(OosOptionsStore *)store
{
	
	[super packStore:store];
	
	// Get the query string
	
	self.searchQuery = [[store queryOptions] query];
	
	// Get Location options
	
	OosLocationOptions *locationOptions = store.locationOptions;
	self.areaSlug = locationOptions.area;
	self.location = locationOptions.location;
	self.latitude = locationOptions.latitude;
	self.longitude = locationOptions.longitude;
	self.radius = locationOptions.radius;
	self.boundaryBox = [locationOptions.bbox description];
	
	//Get FilterOptions
	
	OosFilterOptions *filterOptions = store.filterOptions;
	self.tags = [NSStringUtils stringWithValues: filterOptions.tags];
	self.categories = [NSStringUtils stringWithValues: filterOptions.categories];
	self.attributes = [NSStringUtils stringWithValues: filterOptions.attributes];
	self.categoryGroups = [NSStringUtils stringWithValues: filterOptions.categoryGroups];
	
	//Get Scope options
	
	OosScopeOptions *scopeOptions = store.scopeOptions;
	self.scope = [scopeOptions.scope description];
	
	// Get Order options
	
	OosOrderOptions *orderOptions = store.orderOptions;
	self.order = [orderOptions.order description];
    
    [self addCustomParameterWithName:REDUCED_KEY  value:@"true"];
	
}

@end
